package com.teamcalendar.client.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.teamcalendar.api.CreateJson;
import com.teamcalendar.client.DBAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class ReadMessageActivity extends Activity {
	
	List<HashMap<String, String>> messages = new ArrayList<HashMap<String, String>>();
	String senderId;
	String senderFirstName;
	String senderLastName;
	String message;
	String memberId;
	long rowId;
	DBAdapter mDb = new DBAdapter(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.read_message);
		mDb.open();
		
		Bundle bundle = getIntent().getExtras();
		rowId = bundle.getLong("rowid");
		memberId = bundle.getString("memberId");
		
		Cursor messageCursor = mDb.getMessage(rowId);
		message = messageCursor.getString(messageCursor.getColumnIndex(DBAdapter.KEY_MESSAGE));
		senderId = messageCursor.getString(messageCursor.getColumnIndex(DBAdapter.KEY_SENDERID));
		messageCursor.deactivate();
		
		Cursor memberCursor = mDb.getMyTeamMember(senderId);
		senderFirstName = memberCursor.getString(memberCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME));
		senderLastName = memberCursor.getString(memberCursor.getColumnIndex(DBAdapter.KEY_LASTNAME));
		memberCursor.deactivate();
		
		TextView tvText = (TextView) findViewById(R.id.read_message_text);
		tvText.setText(message);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	};
	
	public void answer() {
		Intent writeMessage = new Intent(ReadMessageActivity.this, WriteMessageActivity.class);
		writeMessage.putExtra("message", message);
		writeMessage.putExtra("memberId", memberId);
		writeMessage.putExtra("senderId", senderId);
		startActivity(writeMessage);
	}
	
	private void delete() {
		Cursor c = mDb.getMessage(rowId);
		CreateJson json = new CreateJson();
		json.setJsonObjectType("deletemessage");
		json.setMemberId(memberId);
		json.setMessageId(c.getString(c.getColumnIndex(DBAdapter.KEY_MESSAGE_ID)));
		json.createJsonObject();
		c.deactivate();
		mDb.deleteMessage(rowId);
		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.read_message_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_read_message_answer:
			answer();
			return true;
		case R.id.menu_read_message_delete:
			delete();
			return true;
		
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		
		menu.add(0, R.id.menu_read_message_answer, 0, "Svara");
		menu.add(0, R.id.menu_read_message_delete, 0, "Ta bort");
		return super.onPrepareOptionsMenu(menu);
	}
}
